package 图论;

/**
 * @author TTDB
 * @create 2025/9/18
 */
public class numIslands {
     public int numIslands(char[][] grid) {
      int ans = 0;
      for(int i = 0; i < grid.length; i++){
          for(int j = 0; j < grid[0].length; j++){
              if(grid[i][j] == '1'){
                  ans++;
                  dfs(grid, i, j);
              }
          }
      }
      return ans;
    }
    public void dfs(char[][] grid, int i, int j){
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != '1') return;
        grid[i][j] = '0';//插个旗子
        dfs(grid, i + 1, j);
        dfs(grid, i - 1, j);
        dfs(grid, i, j + 1);
        dfs(grid, i, j - 1);
    }
}
